<?php
/**
 * banner
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use app\library\Flag;
use think\Model;
use think\Db;

class Banner extends Model
{
    //set talbename
    protected $table = 'banner';
    //set primary key
    protected $pk = 'banner_id';

    /**
     * @desc 添加Banner
     * @param $param
     * @return false|int
     */
    public function addBanner($param)
    {
        $this->allowField(true)->save($param);
        return $this->data[$this->pk];
    }

    public function updateBanner($banner_id, $param)
    {
        $conds = array(
            'banner_id' => $banner_id,
        );
        $field = array();
        if (isset($param['name'])) {
            $field['name'] = $param['name'];
        }

        if (isset($param['status'])) {
            $field['status'] = $param['status'];
        }

        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    /**
     * @desc 获取Banner
     * @param $param
     * @return false|int
     */
    public function getBanner()
    {
        $field = '*';
        return $this->field($field)->select();
        //return Db::table($this->table)->field($field)->select();
    }

    public function getTypeAttr($value)
    {
        return Flag::$banner_type[$value];

    }

    /**
     * 获取banner
     *
     * @param $type
     * @param int $status
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getBannerByType($type, $status = Flag::CONST_BANNER_STATUS_ON)
    {
        $conds = array(
            'type' => ['=', $type],
            'status' => ['=', $status]
        );
        return Db::table($this->table)->where($conds)->select();
    }
}
